<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排名列表</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/common.css" />
    <link rel="stylesheet" href="../styles/rankings.css" />
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
        }

        #ranking-app {
            max-width: 1400px;  /* 增加最大宽度 */
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .dashboard-container .container {
            padding: 30px;  /* 增加内边距 */
            width: 100%;
            max-width: 800px;  /* 设置最大宽度 */
            margin: 0 auto;
        }

        .search-bar {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            gap: 20px;
        }

        .search-bar el-input {
            width: 250px;
        }

        .tableBox {
            margin-top: 30px;
            margin-bottom: 30px;
            background-color: #f9f9f9;
            border-radius: 8px;
        }

        .tableBox .el-table-column {
            font-size: 14px;
        }

        .tableBox .el-table th {
            background-color: #f0f0f0;
            color: #333;
            text-align: center;
        }

        .tableBox .el-table td {
            text-align: center;
        }

        .button-group {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }

        .button-group el-button {
            width: 48%;
        }

        .pageList {
            text-align: center;
            margin-top: 20px;
        }

        /* 日期和时间选择与按钮区域放在同一行，水平居中 */
        .date-time-buttons-container {
            display: flex;
            justify-content: space-between;
            align-items: center; /* 确保所有元素垂直居中 */
            margin-top: 20px;
            gap: 20px; /* 设置日期、时间与按钮之间的间距 */
        }

        /* 调整日期和时间选择框的宽度 */
        .date-time-buttons-container .el-date-picker,
        .date-time-buttons-container .el-input {
            width: 120px;
        }

        .button-group {
            display: flex;
            justify-content: space-between;
            gap: 15px;
            margin-top: 20px;
            width: 450px;
        }

        .notAdmin::after {
            border: 0 !important;
        }
        /* 添加新的容器样式 */
        .confidence-threshold-container {
            display: flex;
            align-items: center;
            margin-top: 20px;
            gap: 15px;
        }

    </style>
</head>
<body>
<div class="dashboard-container" id="ranking-app">
    <div class="container">
        <div class="search-bar">
            <el-input
                    v-model="nation"
                    placeholder="输入地区"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>

            <el-input
                    v-model="field"
                    placeholder="输入领域"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
<!--            <el-input-->
<!--                    v-model="confidenceT"-->
<!--                    placeholder="输入置信度阈值（0-100）"-->
<!--                    clearable-->
<!--                    @keyup.enter.native="handleQuery"-->
<!--            >-->
<!--                <i-->
<!--                        slot="prefix"-->
<!--                        class="el-input__icon el-icon-search"-->
<!--                        style="cursor: pointer"-->
<!--                        @click="handleQuery"-->
<!--                ></i>-->
<!--            </el-input>-->
            <el-button type="primary" @click="handleQuery">搜索</el-button>
        </div>
        <!-- 添加置信度阈值输入框和确认按钮 -->


        <el-table
                :data="filteredTableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="userRank"
                    label="排名"
            ></el-table-column>
            <el-table-column
                    prop="name"
                    label="名字"
            ></el-table-column>
            <el-table-column
                    prop="score"
                    label="分数"
            ></el-table-column>
            <el-table-column
                    prop="nation"
                    label="地区"
            ></el-table-column>
            <el-table-column
                    prop="field"
                    label="领域"
            ></el-table-column>
        </el-table>

        <el-pagination
                class="pageList"
                :page-sizes="[1, 2, 3, 4, 5, 6, 7, 8]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>

        <!-- 日期、时间选择与按钮区域放在同一行 -->
        <div class="date-time-buttons-container">
            <el-date-picker
                    v-model="selectedDate"
                    type="date"
                    placeholder="选择日期"
                    :disabled-date="disabledDate"
                    :picker-options="datePickerOptions"
                    style="margin-right: 15px; width: 180px;"
            ></el-date-picker>
            <el-input
                    v-model="selectedTime"
                    placeholder="输入小时(0-24)"
                    style="width: 120px;"
                    @blur="validateTime"
            ></el-input>

            <el-button type="success" @click="addDataAndCalculateScores">添加数据并计算分数</el-button>
            <el-button type="primary" @click="guessUserNations">获取与推测新增数据的地区和领域</el-button>
        </div>

    </div>
</div>

<script src="../plugins/vue/vue.js"></script>
<script src="../plugins/element-ui/index.js"></script>
<script src="../plugins/axios/axios.min.js"></script>
<script src="../js/new_request.js"></script>
<script src="../js/validate.js"></script>
<script src="../js/index.js"></script>
<script src="../api/ranking.js"></script>
<script>
    confidenceT = "";
    new Vue({
        el: '#ranking-app',
        data() {
            return {
                nation: '',
                field: '',
                confidenceT: '',
                counts: 0,
                page: 1,
                pageSize: 5,
                selectedDate: '',

                selectedTime: '',

                tableData: [
                    { userRank: 1, name: '小明', score: 100, nation: '中国', field: '人工智能', nationConfidence: 95 },
                    { userRank: 2, name: '小红', score: 95, nation: '美国', field: '数据科学', nationConfidence: 85 },
                    { userRank: 3, name: '小刚', score: 90, nation: '日本', field: '机器学习', nationConfidence: 60 }
                ],
                datePickerOptions: {
                    disabledDate(time) {
                        return time.getFullYear() < 2014 || time > new Date();
                    }
                },
                timePickerOptions: {
                    selectableRange: '10:00:00 - 24:00:00'
                }
            };
        },
        computed: {
            filteredTableData() {
                return this.tableData.map(item => {
                    item.score = item.score.toFixed(3);
                    item.field = item.field && item.field.trim() !== '[]' ? item.field.replace(/\[|\]/g, '').trim() : 'N/A';
                    item.nation = item.nation && item.nation.trim() !== '' ? item.nation : 'N/A';
                    // 判断地区置信度是否低于阈值
                    // if (item.nationConfidence < parseFloat(this.confidenceT)) {
                    //     item.nation = 'N/A';  // 置信度低于阈值时，将地区改为 N/A
                    // }
                    return item;
                });
            }
        },
        created() {
            this.pageSize = 5;
            this.init();
            this.field = "";
            this.nation = "";
            this.confidenceT = "";
        },
        methods: {
            validateTime() {
                const time = parseInt(this.selectedTime);
                if (isNaN(time) || time < 0 || time > 24) {
                    this.$message.error('请输入有效的小时数，范围是0到24');
                    this.selectedTime = '';
                }
            },

            async init() {
                const response = await getRankingData({ page: this.page, pageSize: this.pageSize, nation: this.nation, field: this.field });
                this.tableData = response.data.records;
                this.counts = response.data.total;
            },
            handleQuery() {
                // if (this.confidenceT) {
                //     const threshold = parseInt(this.confidenceT);
                //     if (isNaN(threshold) || threshold < 0 || threshold > 100) {
                //         this.$message.error('请输入有效的置信度阈值，范围是0到100');
                //         return;
                //     }
                // }
                this.page = 1;
                this.init();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init();
            },
            async addDataAndCalculateScores() {
                if (!this.selectedDate || !this.selectedTime) {
                    this.$message.error('请先选择日期和时间');
                    return;
                }

                // 显示加载中提示
                const loading = this.$loading({
                    text: '加载中...',
                    spinner: true,
                    background: 'rgba(0, 0, 0, 0.7)',
                });
                try {
                    const response = await addData({
                        date: this.selectedDate.toISOString().split('T')[0],
                        time: this.selectedTime,
                    });
                    if (response.code === 1) {
                        this.$message.success('数据已成功添加并计算分数！')
                        await this.init();
                    } else {
                        this.$message.error(response.msg || '操作失败')
                    }
                } catch (error) {
                    // 处理错误，例如超时等
                    this.$message.error('请求失败，请稍后再试');
                    console.error(error);
                } finally {
                    // 无论请求成功还是失败，都要关闭加载中提示
                    loading.close();
                }
            },
            async guessUserNations() {
                if (!this.selectedDate || !this.selectedTime) {
                    this.$message.error('请先选择日期和时间');
                    return;
                }
                this.$confirm('由于API访问速率限制，此操作耗时较长！是否继续?', '提示', {
                    confirmButtonText: '继续',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.proceedWithAction();
                }).catch(() => {
                    this.$message.info('操作已取消');
                });
            },
            async proceedWithAction() {
                const response = await guessNation({});
                if (response.code === 1) {
                    this.$message.success('成功获取新增数据地区和领域并猜测未填地区！')
                    await this.init();
                } else {
                    this.$message.error(response.msg || '操作失败')
                }
            },
        }
    });
</script>
</body>
</html>
